package com.fsc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fsc.entity.domain.ChatRecord;
import com.fsc.entity.dto.ChatGetGHistoryDTO;
import com.fsc.entity.dto.ChatGetPHistoryDTO;
import com.fsc.entity.vo.GChatRecordVO;
import com.fsc.entity.vo.MessageTmpVO;
import com.fsc.entity.vo.PChatRecordVO;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 86183
 * @description 针对表【chat_record(聊天内容表)】的数据库操作Mapper
 * @createDate 2024-06-02 13:15:21
 * @Entity com.fsc.domain.ChatRecord
 */
public interface ChatRecordMapper extends BaseMapper<ChatRecord> {
    //一次查15条聊天记录
    @Select("select send_id,receive_id,message_type,message,voice,duration from chat_record " +
            "where (send_id = #{sendId} and receive_id = #{receiveId} and chat_type=0 )" +
            " or (send_id=#{receiveId} and receive_id=#{sendId} and chat_type=0 ) " +
            " order by time DESC  limit #{pos},15")
    List<PChatRecordVO> getHistory(ChatGetPHistoryDTO chatGetPHistoryDTO);

    //只查询一条最新数据
    @Select("select send_id,message,message_type,duration,time " +
            "from chat_record where receive_id=#{id} and send_id=#{sendId} order by time desc limit 1 ")
    MessageTmpVO getPersonMsg(Integer id ,Integer sendId);

    @Select("select r.send_id,r.message,r.message_type,r.duration,r.time" +
            " from chat_record r join group_chat c " +
            " on c.chat_id=r.id where c.group_id=#{id} order by r.time desc limit 1 ")
    MessageTmpVO getGroupMsg(Integer id);


    @Select("select a.send_id,b.nick_name,b.icon_url,a.group_id,a.message_type,a.message,a.voice,a.duration  " +
            " from  (select cr.send_id,gc.group_id,cr.message_type,cr.message,cr.voice,cr.duration " +
            " from group_chat gc join chat_record cr on gc.chat_id=cr.id" +
            " where gc.group_id=#{groupId} order by time desc limit #{pos},15 ) a join " +
            "(select gm.member_id,gm.nick_name,li.icon_url  " +
            "from group_member gm join login_info li on gm.member_id=li.id" +
            " where gm.group_id=#{groupId} " +
            ") as b on a.send_id=b.member_id  ")
    List<GChatRecordVO> getGHistory(ChatGetGHistoryDTO chatGetGHistoryDTO);


//    //选出所有好友发送的最后一条信息。
//    @Results(id = "personMsg", value = {
//            @Result(property = "sendId", column = "send_id"),
//            @Result(property = "chatType", column = "chat_type"),
//            @Result(property = "iconUrl", column = "icon_url"),
//            @Result(property = "message", column = "message"),
//            @Result(property = "messageType", column = "message_type"),
//            @Result(property = "duration", column = "duration"),
//            @Result(property = "name", column = "friend_nickname")
//    })
//    @Select("SELECT r.send_id, r.chat_type, k.icon_url, r.message, r.message_type, r.duration, k.friend_nickname " +
//            "FROM chat_record r " +
//            "JOIN (SELECT f.friend_id, f.friend_nickname, l.icon_url " +
//            "      FROM chat_friend f " +
//            "      JOIN login_info l ON f.friend_id = l.id " +
//            "      WHERE f.user_id = #{id}) AS k " +
//            "ON k.friend_id = r.send_id " +
//            "ORDER BY r.time DESC")
//    List<MessageTipVO> getPersonMessage(Integer id);
//
//    @Results(id = "groupMsg", value = {
//            @Result(property = "sendId", column = "send_id"),
//            @Result(property = "chatType", column = "chat_type"),
//            @Result(property = "groupId", column = "group_id"),
//            @Result(property = "iconUrl", column = "group_icon"),
//            @Result(property = "message", column = "message"),
//            @Result(property = "messageType", column = "message_type"),
//            @Result(property = "duration", column = "duration"),
//            @Result(property = "name", column = "group_name")
//    })
//    @Select("SELECT b.group_id, b.send_id, b.chat_type, a.group_icon, b.message, b.message_type, b.duration, a.group_name " +
//            "FROM (SELECT m.group_id, i.group_icon, i.group_name " +
//            "      FROM group_member m " +
//            "      JOIN group_info i ON m.group_id = i.id " +
//            "      WHERE m.member_id = #{id}) a " +
//            "JOIN (SELECT c.group_id, r.send_id, r.chat_type, r.message, r.message_type, r.duration, r.time " +
//            "      FROM group_chat c " +
//            "      JOIN chat_record r ON c.chat_id = r.id " +
//            "      ORDER BY r.time DESC) b " +
//            "ON a.group_id = b.group_id")
//    List<MessageTipVO> getGroupMessage(Integer id);
}




